package com.android.camera;

import com.android.camera.NavAnimationView.AnimatorListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.android.camera.manager.ViewManager;

public class NaviAnimationManager {
	private CameraActivity mActivity;
	private FrameLayout mView;

	private SharedPreferences naviSharedPreferences;
	private static final String NAVI_SHARED_PREFERENCE_NAME = "application_guide";
	private static final String KEY_NAVI_ANIMATION = "navi_anim_done";

	private NavAnimationView mNavAnimationView;
	private boolean isTouchOkBtn = false;

	private static final int MSG_PLAY_NAVI_ANIMATION = 1;

	private boolean visible = false;

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case MSG_PLAY_NAVI_ANIMATION:
				if (mNavAnimationView != null) {
					mNavAnimationView.startAnimation();
				}
				break;
			}
		}
	};

	private AnimatorListener mAnimatorListener = new AnimatorListener() {

		@Override
		public void onAnimationEnd() {
			if (!isTouchOkBtn) {
				mHandler.sendEmptyMessageDelayed(MSG_PLAY_NAVI_ANIMATION, 3000);
			}
		}
	};

	private Button mOkBtn;

	/**
	 * ok button listner, finish app guide.
	 */
	private View.OnClickListener mOkListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.d("sqm", "stop navi animation.");

			naviSharedPreferences.edit().putBoolean(KEY_NAVI_ANIMATION, true)
					.commit();

			isTouchOkBtn = true;

			dismissAppGuide();
		}
	};

	public NaviAnimationManager(CameraActivity activity) {
		mActivity = activity;
	}

	private FrameLayout getView() {
		Log.d("sqm", "NaviAnimationManager[getView]");

		LayoutInflater inflater = LayoutInflater.from(mActivity);
		FrameLayout view = (FrameLayout) inflater.inflate(
				R.layout.navi_anim_layout, null);

		mNavAnimationView = (NavAnimationView) view
				.findViewById(R.id.nav_anim_view);
		mNavAnimationView.setAnimatorListener(mAnimatorListener);

		mOkBtn = (Button) view.findViewById(R.id.nav_ok_btn);
		mOkBtn.setOnClickListener(mOkListener);

		return view;
	}

	public void maybeShowNaviAnimation() {
		naviSharedPreferences = mActivity.getSharedPreferences(
				NAVI_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		if (naviSharedPreferences.getBoolean(KEY_NAVI_ANIMATION, false)) {
			Log.d("sqm",
					"[maybeShowNaviAnimation]already show swipe screen guide, return");
			return;
		}

		visible = true;
		mView = getView();
		mActivity.addView(mView, ViewManager.VIEW_LAYER_OVERLAY);
		mNavAnimationView.startAnimation();
	}

	public void dismissAppGuide() {
		Log.d("sqm", "[dismissAppGuide]");
		if (mNavAnimationView != null) {
			visible = false;
			mNavAnimationView.stopAnimation();
			mNavAnimationView = null;
			//mView.setVisibility(View.GONE);
			mActivity.removeView(mView, ViewManager.VIEW_LAYER_OVERLAY);
			mView = null;
		}

		if (mHandler.hasMessages(MSG_PLAY_NAVI_ANIMATION)) {
			mHandler.removeMessages(MSG_PLAY_NAVI_ANIMATION);
		}
	}

	public boolean visible() {
		return visible;
	}

}
